package com.zp.oj.judge.strategy;

import cn.hutool.json.JSONUtil;
import com.zp.oj.judge.model.ExecuteCodeRequest;
import com.zp.oj.judge.model.ExecuteCodeSandResponse;
import com.zp.oj.model.dto.question.JudgeCase;
import com.zp.oj.model.dto.question.JudgeConfig;
import com.zp.oj.judge.model.JudgeInfo;
import com.zp.oj.model.entity.Question;
import com.zp.oj.model.enums.JudgeInfoEnum;

import java.util.List;

public class JavaJudgeStrategy implements JudgeStrategy {
    private Long JAVA_Time_Config = 1000L;

    public JudgeInfo doJudge(JudgeContent judgeContent, ExecuteCodeSandResponse executeCodeSandResponse) {
        List<String> inputs = judgeContent.getInputs();
        List<String> outPuts = judgeContent.getOutPuts();
        Question question = judgeContent.getQuestion();
        List<JudgeCase> judgeCaseList = judgeContent.getJudgeCaseList();
        // 获取信息
        JudgeInfo judgeInfo = judgeContent.getJudgeInfo();
        JudgeInfoEnum judgeInfoEnum = JudgeInfoEnum.ACCEPT;

        Long time = judgeInfo.getTime() == null ? 0 : judgeInfo.getTime();
        Long memory = judgeInfo.getMemory() == null ? 0 : judgeInfo.getMemory();
        // 封装返回信息
        JudgeInfo judgeInfoResponse = new JudgeInfo();
        judgeInfoResponse.setMessage(judgeInfoEnum.getValue());
        judgeInfoResponse.setTime(time);
        judgeInfoResponse.setMemory(memory);

        // 查看是否編譯成功
        if (memory == 0 || time == 0) {
            judgeInfoEnum = JudgeInfoEnum.COMPILE_ERROR;
            judgeInfoResponse.setMessage(judgeInfoEnum.getValue());
            return judgeInfoResponse;
        }
        // #根据返回结果判断是否正确
        if (outPuts.size() != inputs.size()) {
            judgeInfoEnum = JudgeInfoEnum.WRONG_ANSWER;
            judgeInfoResponse.setMessage(judgeInfoEnum.getValue());
            return judgeInfoResponse;
        }
        for (int i = 0; i < judgeCaseList.size(); i++) {
            JudgeCase judgeCase = judgeCaseList.get(i);
            String output = outPuts.get(i).replaceAll("\n", ""); // 清除回车
            System.out.println(output);
            if (!judgeCase.getOutput().equals(output)) {
                judgeInfoEnum = JudgeInfoEnum.WRONG_ANSWER;
                judgeInfoResponse.setMessage(judgeInfoEnum.getValue());
                executeCodeSandResponse.setMessage("程序输出:" + output + "\n" + "正确输出:" + judgeCase.getOutput());
                return judgeInfoResponse;
            }
        }
            // 判读判题配置;
            String judgeConfigStr = question.getJudgeConfig();
            JudgeConfig judgeConfig = JSONUtil.toBean(judgeConfigStr, JudgeConfig.class);
            Long timeLimit = judgeConfig.getTimeLimit();
            Long memoryLimit = judgeConfig.getMemoryLimit();
            memory = memory / (1000 * 1000);
            if (memoryLimit <= memory) {
                judgeInfoEnum = JudgeInfoEnum.MEMORY_LIMIT_EXCEEDED;
                judgeInfoResponse.setMessage(judgeInfoEnum.getValue());
                return judgeInfoResponse;
            }
            if (timeLimit < time) {
                judgeInfoEnum = judgeInfoEnum.TIME_LIMIT_EXCEEDED;
                judgeInfoResponse.setMessage(judgeInfoEnum.getValue());
                return judgeInfoResponse;
            }
            return judgeInfoResponse;
        }
}
